"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库weight_string函数
Case Name   : weight_string函数在严格模式与非严格模式下测试超长字符，部分测试点合理报错
Create At   : 2023.07.05
Owner       : z@wan005
Description :
    1、创建数据库
    2、入参为超长字符串
    3、清理环境
Expect      :
    1、成功
    2、超出返回warning，超过内存限制，报错
    3、成功
History     :modified by z@wan005，2023.09.20，step2，select场景下不报错，
            优化用例断言
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class CompatibilityTest13(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = "db_uncpmpress_0013"
        self.expect = 'WARNING:  weight_string could not support more ' \
                      'than 1GB data'

    def test_weight_string(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建数据库 expect:成功-----'
        self.log.info(text)
        create_cmd = f"drop database if exists {self.db_name};" \
                     f"create database {self.db_name} dbcompatibility 'b';"
        msg = self.sh_primary.execut_db_sql(create_cmd)
        self.log.info(msg)
        self.assertIn('CREATE DATABASE', msg, '执行失败:' + text)

        text = '--step2:入参为超长字符串 expect:超出返回warning，超过内存限制，报错--'
        self.log.info(text)
        sql_cmd = "select hex(weight_string(rpad('a', 1000000000, 'a') as " \
                  "char(1000000000)));" \
                  "select hex(weight_string(rpad('a', 1000000000, 'a') " \
                  "as binary(1000000000)));"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(msg)
        self.assertIn(self.expect, msg, '执行失败:' + text)
        self.assertIn('ERROR:', msg, '执行失败:' + text)

        sql_cmd = "set dolphin.sql_mode='';" \
                  "select hex(weight_string(rpad('a', 1000000000, 'a') " \
                  "as char(1000000000)));" \
                  "reset dolphin.sql_mode;"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(msg)
        self.assertEqual(0, len(msg.splitlines()[-4].strip()),
                         '执行失败:' + text)
        self.assertIn(self.expect, msg, '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(msg)
        self.assertIn('DROP DATABASE', msg, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
